﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace Jy123.Web.home
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class FriendAddByUserID : IHttpHandler
    {
        private int userID;//用户ID
        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);


            string msg = string.Empty;
            try
            {
                string strBeAccessUser = context.Request["UserID"];

                //Jy123.Model.User_Request mRequest = new Jy123.Model.User_Request();
                //Jy123.BLL.User_Request bllRequest = new Jy123.BLL.User_Request();
                Jy123.Model.User_Friend mFriend = new Jy123.Model.User_Friend();
                Jy123.BLL.User_Friend bllFriend = new Jy123.BLL.User_Friend();
                mFriend.FUser = userID;

                int isRequested = new BLL.User.Friend().AlreadySearch(userID, Convert.ToInt32(strBeAccessUser));
                switch (isRequested)
                {
                    case 2:
                        mFriend.FFriend = Convert.ToInt32(strBeAccessUser);//被请求者ID
                        mFriend.State = 0;
                        mFriend.AddTime = DateTime.Now;
                        bllFriend.Add(mFriend);
                        msg = "请求发送成功!,ok";
                        //加入动态
                        Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                        Jy123.Model.User_Main mUserFrdForState = new Jy123.Model.User_Main();
                        Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                        mUserForState = bllUserForState.GetModel(userID);
                        mUserFrdForState = bllUserForState.GetModel(Convert.ToInt32(strBeAccessUser));
                        new Jy123.BLL.Public.State().CreateMakeFriendState(mUserForState, mUserFrdForState, (int)Jy123.BLL.Public.EnumConfig.StateActionType.ApplyFriend);
                        //加入动态
                        break;
                    case 0:
                        msg = "等待好友确认中!,warning";
                        break;
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }

            context.Response.ContentType = "text/plain";
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
